import { removeCookie } from '~/utils/cookie'
import { menus, auth } from '~/utils/router'

export const state = () => {
  return {
    isLogin: false,
    userInfo: {},
    userMenus: [],
    userPermission: null
  }
}

export const mutations = {
  // 储存用户信息
  saveUserInfo (state, data) {
    state.isLogin = true
    state.userInfo = data
    state.userMenus = menus(data.roles)
    state.userPermission = auth(data.roles)
  },
  // 清除用户信息
  clearUserInfo (state) {
    state.isLogin = false
    state.userInfo = {}
    state.userMenus = []
    state.userPermission = null
    removeCookie('token')
  }
}

export const actions = {
  // 储存用户信息
  saveUserInfo ({ commit }, data) {
    commit('saveUserInfo', data)
  },
  // 清除用户信息
  clearUserInfo ({ commit }) {
    commit('clearUserInfo')
  }
}
